package com.shenhan.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author shenhan
 * @Date: 2020/6/13 12:54
 * @Description:
 */
public class Demo2 {
    public static void main(String[] args) {
        Phone1 phone = new Phone1();
        new Thread(phone::sms, "A").start();
        new Thread(phone::sms, "B").start();
    }
}

class Phone1 {
    private static final Lock LOCK = new ReentrantLock();
    public void sms() {
        LOCK.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "smc");
            call();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally {
            LOCK.unlock();
        }
    }

    public void call() {
        LOCK.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "call");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally {
            LOCK.unlock();
        }
    }
}